
let bookmarksDataCache = []
window.exports = {
    'bookmarksSearch': {
        enter: (action, callbackSetList) => {
            bookmarksDataCache = window.api.getBookmarks()
        },
        search: (action, searchWord, callbackSetList) => {
            searchWord = searchWord.trim()
            if (!searchWord) return callbackSetList()
            if (/\S\s+\S/.test(searchWord)) {
                const regexTexts = searchWord.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').split(/\s+/)
                const searchRegexs = regexTexts.map(rt => new RegExp(rt, 'i'))
                return callbackSetList(bookmarksDataCache.filter(x => (
                    !searchRegexs.find(r => x.extInfo.title.search(r) === -1) || !searchRegexs.find(r => x.extInfo.description.search(r) === -1)
                )))
            } else {
                const regexText = searchWord.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
                const searchRegex = new RegExp(regexText, 'i')
                return callbackSetList(bookmarksDataCache.filter(x => (
                    x.extInfo.title.search(searchRegex) !== -1 || x.extInfo.description.search(searchRegex) !== -1
                )))
            }
        },
        select: (action, itemData) => {
            if (itemData.extInfo.browser === 'chrome') {
                window.api.openUrlByChrome(itemData.extInfo.url)
            }
        }

    }
}
